//package legorobot;

/**
 *
 * @author Daniel Van Eck
 */
public class CommandStack {
    //Define constants
    private static final int MAX_CMDS = 400;
    
    //place to store stack of commands
    private Command[] cmds;
    
    //current pos in the array
    private int pos;
    
    //create a new array of cmds
    public CommandStack() {
        cmds = new Command[MAX_CMDS];
        for(int i = 0; i < MAX_CMDS; i++)
            cmds[i] = new NullCmd(null);
        pos = 0;
    }
    
    //empty out the stack
    /*public void clearStack() {
        cmds = new Command[MAX_CMDS];
        for(int i = 0; i < MAX_CMDS; i++)
            cmds[i] = new NullCmd(null);
        pos = 0;
    }*/
    
    /*
     * Adds a new Command to the top of the stack
     * Removes the Command at the bottom of the stack
     */
    public void pushCommand(Command c) {
        if( pos >= MAX_CMDS ) {
            //stack is full, drop the last cmd and push everything down
            for(int i = 0; i < (MAX_CMDS - 2); i++)
                cmds[i] = cmds[i+1];
            pos--;
        }
        
        cmds[pos++] = c;
        
        //shift all old commands down the stack
        /*for(int i = (MAX_CMDS - 2); i >= 0; i--)
            cmds[i+1] = cmds[i];
        cmds[0] = c; // store the new cmd at the top of the stack*/
    }
    
    /*
     * Returns the top Command in the stack and removes it from the stack
     */
    public Command popCommand() {
        if(pos < 1) {
            return null;
        }
        Command poppedCmd = cmds[--pos];
        cmds[pos] = new NullCmd(null);
        return poppedCmd;
        
        /*
        Command poppedCmd = cmds[0];
        
        //Shift all the cmds up the stack
        for(int i = 0; i < (MAX_CMDS - 1); i++)
            cmds[i] = cmds[i+1];
        
        //Add a null command to the bottom        
        cmds[MAX_CMDS - 1] = new NullCmd(null);
        
        return poppedCmd;*/
    }
}
